﻿using System;
using System.Linq;
using System.Web.Caching;
using System.Xml.Linq;
using Netkk.Common;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Models;
using System.Collections.Generic;

namespace Netkk.Data.Repositories
{
    public class RoleRepository : BaseRepository, IRoleRepository
    {
        private readonly string rolePath;
        
        protected XDocument RoleDocument
        {
            get { return CacheWrapper.Load("roles", new CacheDependency(rolePath), () => XDocument.Load(rolePath)); }
        }

        public RoleRepository()
        {
            rolePath = string.Concat(ConnectionString, "Roles.xml");
        }

        public IEnumerable<UserRole> Select()
        {
            IEnumerable<UserRole> roles = from r in RoleDocument.Element("Roles").Elements("Role")
                                          select getRoleFromXElement(r);
            return roles;
        }

        private UserRole getRoleFromXElement(XElement XRole)
        {
            return new UserRole
            {
                Id = XRole.Element("Id").Value.ToOrDefault<Guid>(),
                Role = XRole.Element("Name").Value.ToOrDefault<Roles>()
            };
        }

        public void Delete(UserRole content)
        {
            throw new NotImplementedException();
        }

        public void Save(UserRole content)
        {
            throw new NotImplementedException();
        }

    }

}
